package com.cn.jinl.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cn.jinl.api.IOrderService;
import com.cn.jinl.domain.InventoryVo;
import com.cn.jinl.domain.Order;
import com.cn.jinl.utils.CommonUtil;
import com.cn.jinl.utils.JsonResponse;
import com.cn.jinl.utils.QueryForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.net.URLDecoder;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private IOrderService orderService;

    @RequestMapping("/getOrderList/{frontToken}")
    @ResponseBody
    public JsonResponse<List<Order>> getOrderList(@PathVariable(value = "frontToken",required = true) String frontToken, @RequestBody QueryForm queryForm) throws Exception{
        JsonResponse<List<Order>> response = new JsonResponse<>();
        List<Order> orders = orderService.getOrderList(queryForm);
        response.setResult(orders);
        response.setSuccess(true);
        return response;
    }

    @RequestMapping("/getOrderGrades/{frontToken}")
    @ResponseBody
    public JsonResponse<List<String>> getOrderGrades(@PathVariable(value = "frontToken",required = true) String frontToken) throws Exception{
        JsonResponse<List<String>> response = new JsonResponse<>();
        List<String> orders = orderService.getOrderGrades();
        response.setResult(orders);
        response.setSuccess(true);
        return response;
    }

    @RequestMapping("/getOrderNum/{frontToken}")
    @ResponseBody
    public JsonResponse<Map<String,Integer>> getOrderNum(@PathVariable(value = "frontToken",required = true) String frontToken) throws Exception{
        JsonResponse<Map<String,Integer>> response = new JsonResponse<>();
        Map<String,Integer> orderNumMap = orderService.getOrderNum();
        response.setResult(orderNumMap);
        response.setSuccess(true);
        return response;
    }

    @RequestMapping("/getOrderPlanNum/{frontToken}")
    @ResponseBody
    public JsonResponse<Map<String,Integer>> getOrderPlanNum(@PathVariable(value = "frontToken",required = true) String frontToken) throws Exception{
        JsonResponse<Map<String,Integer>> response = new JsonResponse<>();
        Map<String,Integer> orderNumMap = orderService.getOrderPlanNum();
        response.setResult(orderNumMap);
        response.setSuccess(true);
        return response;
    }

    @RequestMapping("/addBatchOrder/{frontToken}")
    @ResponseBody
    public JsonResponse<String> addBatchOrder(@RequestBody List<Order> orders, @PathVariable(value = "frontToken",required = true) String frontToken) throws Exception{
        JsonResponse<String> response = new JsonResponse<>();
        String result = orderService.addBatchOrder(orders);
        return CommonUtil.getResponseData(response,result);
    }

    @RequestMapping("/updateOrder/{frontToken}")
    @ResponseBody
    public JsonResponse<String> updateOrder(@RequestBody Order order, @PathVariable(value = "frontToken",required = true) String frontToken) throws Exception{
        JsonResponse<String> response = new JsonResponse<>();
        String result = orderService.updateOrder(order);
        return CommonUtil.getResponseData(response,result);
    }

    //规格确认
    @RequestMapping("/updateSureOrder/{frontToken}")
    @ResponseBody
    public JsonResponse<String> updateSureOrder(@RequestBody Order order, @PathVariable(value = "frontToken",required = true) String frontToken) throws Exception{
        JsonResponse<String> response = new JsonResponse<>();
        String result = orderService.updateSureOrder(order);
        return CommonUtil.getResponseData(response,result);
    }

    @RequestMapping("/updateCompleteOrder/{frontToken}")
    @ResponseBody
    public JsonResponse<String> updateSureOrder(String id, @PathVariable(value = "frontToken",required = true) String frontToken) throws Exception{
        JsonResponse<String> response = new JsonResponse<>();
        String result = orderService.updateCompleteOrder(id);
        return CommonUtil.getResponseData(response,result);
    }

    //需求下发
    @RequestMapping("/updateSendNeedOrder/{frontToken}")
    @ResponseBody
    public JsonResponse<String> updateSureOrder(@RequestBody List<Order> orders, @PathVariable(value = "frontToken",required = true) String frontToken) throws Exception{
        JsonResponse<String> response = new JsonResponse<>();
        String result = orderService.updateSendNeedOrder(orders);
        return CommonUtil.getResponseData(response,result);
    }

    @RequestMapping("/deleteOrders/{frontToken}")
    @ResponseBody
    public JsonResponse<String> deleteOrders(String id, @PathVariable(value = "frontToken",required = true) String frontToken) throws Exception{
        JsonResponse<String> response = new JsonResponse<>();
        String result = orderService.deleteOrders(id);
        return CommonUtil.getResponseData(response,result);
    }

    //订单录入客户下拉自匹配
    @RequestMapping("/getNameByQuery/{frontToken}")
    @ResponseBody
    public JsonResponse<List<Order>> getNameByQuery(@RequestBody String queryString,@PathVariable(value = "frontToken",required = true) String frontToken) throws Exception{
        JsonResponse<List<Order>> response = new JsonResponse<>();
        List<Order> orders = orderService.getNameByQuery(queryString);
        response.setResult(orders);
        response.setSuccess(true);
        return response;
    }

    //作废
    @RequestMapping("/cancelOrder/{frontToken}")
    @ResponseBody
    public JsonResponse<String> cancelOrder(@RequestBody QueryForm queryForm,@PathVariable(value = "frontToken",required = true) String frontToken) throws Exception{
        JsonResponse<String> response = new JsonResponse<>();
        String result = orderService.cancelOrder(queryForm);
        return CommonUtil.getResponseData(response,result);
    }

    //获取库存数据
    @RequestMapping("/getInventory/{frontToken}")
    @ResponseBody
    public JsonResponse<List<Order>> getInventory(@PathVariable(value = "frontToken",required = true) String frontToken, @RequestBody QueryForm queryForm) throws Exception{
        JsonResponse<List<Order>> response = new JsonResponse<>();
        List<Order> orders = orderService.getInventory(queryForm);
        response.setResult(orders);
        response.setSuccess(true);
        return response;
    }


    //获取库存跟报工数据,(报工数据-库存数据)计算当前库存
    @RequestMapping("/getCurrentInventory/{frontToken}")
    @ResponseBody
    public JsonResponse<List<InventoryVo>> getCurrentInventory(@PathVariable(value = "frontToken",required = true) String frontToken,@RequestBody QueryForm queryForm) throws Exception{
        JsonResponse<List<InventoryVo>> response = new JsonResponse<>();
        List<InventoryVo> inventorys = orderService.getCurrentInventory(queryForm);
        response.setResult(inventorys);
        response.setSuccess(true);
        return response;
    }
    // 库存详情下载
    @ResponseBody
    @RequestMapping("/exportInventoryInfo")
    public void exportInventoryInfo(@RequestParam(value = "queryJson",required = true) String queryJson, HttpServletResponse response) throws Exception{
        QueryForm queryForm = new QueryForm();
        JSONObject jsonObject = JSON.parseObject(queryJson);
        queryForm.setItemId(URLDecoder.decode(jsonObject.getString("itemId"), "UTF-8"));
        queryForm.setName(URLDecoder.decode(jsonObject.getString("name"), "UTF-8"));
        orderService.exportInventoryInfo(queryForm,response);
    }


    //设置库存数为0 平衡库存数
    @RequestMapping("/api/balancedInventory")
    @ResponseBody
    public JsonResponse<String> balancedInventory() throws Exception{
        JsonResponse<String> response = new JsonResponse<>();
        String result = orderService.balancedInventory();
        return CommonUtil.getResponseData(response,result);
    }

    @RequestMapping("/getOrderCount/{frontToken}")
    @ResponseBody
    public JsonResponse<List<Order>> getOrderCount(@PathVariable(value = "frontToken",required = true) String frontToken, @RequestBody QueryForm queryForm) throws Exception{
        JsonResponse<List<Order>> response = new JsonResponse<>();
        List<Order> orders = orderService.getOrderCount(queryForm);
        response.setResult(orders);
        response.setSuccess(true);
        return response;
    }
}
